import qs from 'qs'
import { loading, cookie } from './wx-tools'

export const domain = 'https://store.028wkf.cn/gateway'
// export const domain = 'http://liyang.vipgz2.idcfengye.com'
// export const domain = 'https://akk.028wkf.cn/gateway'
// export const domain = 'https://192.168.0.119:9999'
// export const domain = 'http://192.168.0.148:9999' // 李阳
// export const domain = 'http://192.168.0.162:9999' // 钟平

const pack = (url, data, params, method) =>
  new Promise((resolve, reject) => {
    loading.show()

    let realURL = ''
    let query = qs.stringify(params, { addQueryPrefix: true, skipNulls: true })
    if (/^https?:\/\//.test(url)) {
      realURL = url + query
    } else {
      realURL = domain + url + query
    }

    let myCookie = ''
    if (cookie.get('my-cookie')) {
      myCookie = cookie.get('my-cookie')
    } else {
      myCookie = ''
    }

    wx.request({
      url: realURL,
      data,
      method,
      dataType: 'json',
      header: {
        'Content-Type': 'application/json;charset=utf-8',
        'X-Requested-With': 'XMLHttpRequest',
        Cookie: myCookie
      },
      success: (res) => {
        loading.close()
        resolve(res)
      },
      fail: (err) => {
        loading.close()
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
        reject(err)
      }
    })
  })

export const $get = (url, params = null) => pack(url, null, params, 'GET')

export const $post = (url, data = null, params = null) =>
  pack(url, data, params, 'POST')

export const $put = (url, data = null, params = null) =>
  pack(url, data, params, 'PUT')

export const $delete = (url, params = null) => pack(url, null, params, 'DELETE')

export const $upload = (url, filePath, name, params = null) =>
  new Promise((resolve, reject) => {
    let realURL = ''
    let query = qs.stringify(params, { addQueryPrefix: true, skipNulls: true })

    let myCookie = ''
    if (cookie.get('my-cookie')) {
      myCookie = cookie.get('my-cookie')
    } else {
      myCookie = ''
    }

    if (/^https?:\/\//.test(url)) {
      realURL = url + query
    } else {
      realURL = domain + url + query
    }

    wx.uploadFile({
      url: realURL,
      filePath,
      name,
      header: {
        'Content-Type': 'multipart/form-data',
        Accept: 'application/json',
        Cookie: myCookie
      },
      success: (res) => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      }
    })
  })

export const $uploadImg = (url, name, params = null) =>
  new Promise((resolve, reject) => {
    loading.show()

    let realURL = ''
    let query = qs.stringify(params, { addQueryPrefix: true, skipNulls: true })

    let myCookie = ''
    if (cookie.get('my-cookie')) {
      myCookie = cookie.get('my-cookie')
    } else {
      myCookie = ''
    }

    if (/^https?:\/\//.test(url)) {
      realURL = url + query
    } else {
      realURL = domain + url + query
    }

    wx.chooseImage({
      count: 1,
      success: (res) => {
        let filePath = res.tempFilePaths[0]
        wx.uploadFile({
          url: realURL,
          filePath,
          name,
          header: {
            'Content-Type': 'multipart/form-data',
            Accept: 'application/json',
            Cookie: myCookie
          },
          success: (res) => {
            loading.close()
            resolve(res)
          },
          fail: (err) => {
            loading.close()
            reject(err)
          }
        })
      },
      fail: (err) => {
        loading.close()
        reject(err)
      }
    })
  })

export const $uploadImg2 = (url, name) =>
  new Promise((resolve, reject) => {
    let realUrl = ''
    if (/^https?:\/\//.test(url)) {
      realUrl = url
    } else {
      realUrl = domain + url
    }

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: (res) => {
        let filePath = res.tempFilePaths[0]
        wx.uploadFile({
          url: realUrl,
          filePath,
          name,
          header: {
            'Content-Type': 'multipart/form-data',
            Accept: 'application/json'
          },
          success: (res) => {
            resolve(res)
          },
          fail: (err) => {
            reject(err)
          }
        })
      },
      fail: (err) => {
        reject(err)
      }
    })
  })

export const readJSON = (filePath) => {
  let fullURL = ''
  if (/^(https?:)?\/\/\w+/.test(filePath)) {
    fullURL = filePath
  } else {
    fullURL = domain.replace(/\/gateway.*/, '') + filePath
  }

  return $get(fullURL)
}
